var app = new Vue({
	el: '#app',
	data: {
		roleTable: [],
		editForm: false,
		roleInfo: [],
		roleName: '',
		privList: [],
		roleId: '',
		addRole: false,
		add_warn: false,
		edit_warn: false
	},
	mounted: function(){
		this.initTable();
	},	
	
	methods: {
		initTable: function(){
			var that = this;
			tools.ajaxRequestAsync("../role/getRoleList", {}, function(data){
				if (data.respCode == 0 && (data.rolelist.list!=undefined)){
					that.roleTable = data.rolelist.list; 
				}else{
					that.showError(data.respDesc);
					return false;
				}			
			});
			
		},
		
		add: function(){
			var that = this;
			if(that.roleName.trim()==""){
				that.add_warn = true;
				return false;
			}			
			tools.ajaxRequestAsync("../role/roleInfoSave",{"roleName": that.roleName},function(data){
        		if (data.respCode != 0) {
        			that.showError(data.respDesc);
        		}else{
  		          that.$message({
		        	showClose: true,
		        	type: 'success',
		            message: '添加成功'
		          });
  		          that.addRole = false;
  		          that.roleName = "";
  		          that.initTable();
        		}
			});	
			
		},
		
		edit: function(idx,row){
			var that =this;
			this.editForm = true;
			this.edit_warn = false;
			this.roleId = row.role_id;
			tools.ajaxRequestAsync("../role/roleEdit",{roleId: row.role_id},function(data){
				console.log(data);
				if (data.respCode != 0&&(data.privGroupList==undefined?false:data.privGroupList.length)) {
					that.showError(data.respDesc);
				}else{
					that.privList = [];
					that.roleName = data.roleName;
					that.roleInfo = data.privGroupList;	
					for(var i=0;i<data.privList.length;i++){
						that.privList.push(data.privList[i].priv_code); 
					}
				}
			});			
		},
		
		cancel: function(){
			this.editForm = false;
			this.roleName = '';
			this.roleId='';
			this.addRole = false;
			this.add_warn= false;
			this.edit_warn = false;
		},
		
		save: function(){
			var that = this;
			if(that.roleName.trim()==""){
				that.edit_warn = true;
				return false;
			}
			var ret  = tools.ajaxRequest("../role/rolePrivSave", {
				"roleName": that.roleName,
				"roleId" : that.roleId,
				"privCodes" : that.privList.join(","),
			});
			if(ret.respCode=="0"){
				that.$message({
				        	showClose: true,
				        	type: 'success',
				            message: '保存成功'
				});
				that.editForm = false;
				that.initTable();
				that.roleName = '';
				that.roleId='';
			}else{
				that.showError(ret.respDesc);
			}					
			
		},
		
		
		
		del: function(idx,row){
			var that = this;
			this.$confirm('此操作将永久删除角色 '+row.role_name+' , 是否继续?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		          tools.ajaxRequestAsync("../role/roleDelete",{roleId: row.role_id},function(data){
		        	  if (data.respCode != 0) {
		        		  that.showError(data.respDesc);
		        	  }else{
		        		  that.$message({
		  		        	showClose: true,
		  		            type: 'success',
		  		            message: '删除成功!'
		  		          });
		  		          that.initTable();
		        	  }		        	  
		          });   
		          
		        }).catch(() => {    
		        });
			
		},
		
		showError:function(info){
			var i = info==undefined?"初始化数据失败":info;
			this.$alert(i, '系统错误', {
		          confirmButtonText: '确定'
		    });			
		}
		
	}
	
});